--[[
每隔一定时间创建一个到处跑的挑战怪在目标区域

]]
local challenge_map = {}

local mt = {}

mt.sum = 0
mt.count = 0
mt.max_count = 3
mt.cool = 0

--[等待添加挑战怪]
function mt:wait_challenge(time)
    if self._wait_timer then
        return
    end
    local cool = time or self.cool
    self._wait_timer = ac.wait(cool*1000,function()
        self._wait_timer:remove()
        self._wait_timer = nil
        self:add_challenge()
    end)
end

--[添加挑战怪]
function mt:add_challenge()
    local player = self.player
    local point = player.enemy_point - {math.random(360),math.random(1000,1500)}
    local unit = player:create_enemy(self.name,point)
    unit.owner_player = player
    unit:add_buff '等待挑战'{point = point}
    self.units[unit.handle] = unit
    self.count = self.count + 1
    if self.count<self.max_count then
        self:wait_challenge()
    end
    unit:event '单位-死亡'(function()
        self.count = self.count - 1
        self.sum = self.sum + 1
        self.units[unit.handle] = nil
        self:wait_challenge()
    end)
end

--[移除]
function mt:remove()
    if self.removed then
        return
    end
    self.removed = true
    if self._wait_timer then
        self._wait_timer:remove()
    end
    for _,unit in ipairs(self.units) do
        unit:remove()
    end
end

--[初始化]
function mt:init()
    self.units = {}
    self:wait_challenge(self.start)
    challenge_map[#challenge_map + 1] = self
end

--[注册挑战]
function ac.register_challenge(player,name,start,cool)
    local data = setmetatable({
        player = player,
        name = name,
        cool = cool,
        start = start,
    }, {__index = mt})
    data:init()
    return data
end



ac.game:event '游戏-清除挑战'(function()
    for _,cle in ipairs(challenge_map) do
        cle:remove()
    end
end)


ac.buff('等待挑战')
{
    on_add = function(self)
        local unit = self.owner
        unit['可挑战'] = true
        unit.ui_title = ('%s(|cfffff000A我挑战|r)'):format(unit.name)
        self:on_move()
        unit_add_life2(unit)
        unit:set_owner(ac.player[16])
        unit:add_restriction '免伤'
        unit:add_restriction '幽灵'
        self:gc(unit:event '单位-被挑战'(function(trg,_,source)

            local name = unit.name
            local player = unit.owner_player
            local point = unit:get_point()
            ac.timer(500,5,function()
                player:create_enemy(unit.name,point)
            end)
            
            self:remove()
            unit:remove()
        end))
        self:gc(function()
            unit['可挑战'] = nil
            if self._move_timer then
                self._move_timer:remove()
            end
            unit:remove_restriction '幽灵'
            unit:remove_restriction '免伤'
        end)
    end,
    on_move = function(self)
        local unit = self.owner
        local player = unit.owner_player
        local point1 = unit:get_point()
        local point2 = player.enemy_point - {math.random(360),math.random(1000,1500)}
        local range = point1 * point2
        local time = range / unit:get('移动速度') + 5
        unit:issue_order('smart',point2)
        if self._move_timer then
            self._move_timer:remove()
        end
        self._move_timer = ac.wait(time*1000,function()
            self:on_move()
        end)
    end,
}



local function challenge_unit(player,target)
    return target and target['可挑战'] and target.owner_player == player
end

register_hero(function(player,hero)
    
    local info = {}
    info[#info + 1] = ac.register_challenge(player,'金币怪',30,90)
    info[#info + 1] = ac.register_challenge(player,'杀敌怪',30,120)
    info[#info + 1] = ac.register_challenge(player,'经验怪',120,180)
    
    player:event '玩家-离开游戏'(function(_,player)
        for _,cle in ipairs(info) do
            cle:remove()
        end
    end)

    hero:event '单位-发布命令'(function(_,_,id,target)
        local order = Order[id]
        if order=='attack' and challenge_unit(player,target) then
            target:notify('单位-被挑战',target,hero)
        end
    end)

    player._tz_time = 0
    player:event '玩家-选择单位'(function(_,_,target)
        if challenge_unit(player,target) then
            local clock = ac.clock()
            if clock-player._tz_time<0.4*1000 then
                target:notify('单位-被挑战',target,hero)
            end
            player._tz_time = clock
        end
    end)
end)


